// pages/login/login.js
import { config } from '../../config.js'
const app = getApp()

var startX, endX;

var moveFlag = true;// 判断执行滑动事件

Page({

  /**
   * 页面的初始数据
   */
  data: {
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
    page: 1,
    ani1: '',
    ani2: '',
    isLogin:false,
    userInfo:null,
    authorized:false
  },
  wxleftLogin(){
    this.setData({
      authorized:true
    })
  },

  //测试放开登录
  login(){
    console.log('登录')
    if (this.authorized){
       this.wxLogin()
    }else{
      this.setData({
        isLogin:true
      })
    }
  },

  wxLogin(){
    wx.login({
      success(res) {
        console.log(res)
        if (res.code) {
          //发起网络请求
          wx.request({
            url: config.api_base_url + '/user-info/login',
            data: {
              'code': res.code
            },
            success:(res)=>{
              console.log(res)
              const openId = res.data.openid
              wx.setStorageSync('openId', openId)
              const sessionKey = res.data.sessionKey
              wx.reLaunch({
                url: '/pages/home/home',
              })
              // wx.request({
              //   url: 'http://10.10.10.102:8080/nxShop/user-info/info',
              //   data: {
              //     'sessionKey': sessionKey,
              //     'signature': datils.signature,
              //     'rawData': datils.rawData,
              //     'encryptedData': datils.encryptedData,
              //     'iv': datils.iv
              //   },
              //   success: (res) => {
              //     console.log(res)
              //   }
              // })
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  /**
   * 取消授权
   */
  cancelAuthorized(){
    this.setData({
      authorized : false
    })
  },

  /**
   * 是否授权
   */
  userAuthorized() {
    wx.getSetting({
      success: data => {
        if (data.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: data => {
              this.setData({
                authorized: true,
                userInfo: data.userInfo
              })
              
              wx.reLaunch({
                url: '/pages/home/home',
              })
            }
          })
        }
      }
    })
  },


  onGetUserInfo(event) {
    const userInfo = event.detail.userInfo
    if (userInfo) {
      wx.setStorageSync('userInfo', userInfo)
      this.wxLogin(event.detail)
      this.setData({
        userInfo,
        authorized: true
      })
    }else{
      this.setData({
        authorized: false,
        isLogin:false
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.userAuthorized()

  },
  /**
   * 点击事件
   */
  leftClick: function(){
    this.move2right();

    moveFlag = false;
  },
  rightClick: function(){
    this.move2left();

    moveFlag = false;
  },

  touchStart: function (e) {

    startX = e.touches[0].pageX; // 获取触摸时的原点

    moveFlag = true;

  },

  // 触摸移动事件

  touchMove: function (e) {

    endX = e.touches[0].pageX; // 获取触摸时的原点

    if (moveFlag) {

      if (endX - startX > 50) {

        console.log("move right");

        this.move2right();

        moveFlag = false;

      }

      if (startX - endX > 50) {

        console.log("move left");

        this.move2left();

        moveFlag = false;

      }

    }

  },

  // 触摸结束事件

  touchEnd: function (e) {

    moveFlag = true; // 回复滑动事件

  },

  //向左滑动操作

  move2left() {

    var that = this;

    if (this.data.page == 2) {

      return

    }

    var animation = wx.createAnimation({

      duration: 1000,

      timingFunction: 'ease',

      delay: 100

    });

    animation.opacity(0.2).translate(-500, 0).step()

    this.setData({

      ani1: animation.export()

    })


    setTimeout(function () {

      that.setData({

        page: 2,

        ani2: ''

      });

    }, 500)

  },

  //向右滑动操作

  move2right() {

    var that = this;

    if (this.data.page == 1) {

      return

    }

    var animation = wx.createAnimation({

      duration: 1000,

      timingFunction: 'ease',

      delay: 100

    });

    animation.opacity(0.2).translate(500, 0).step()

    this.setData({

      ani2: animation.export()

    })

    setTimeout(function () {

      that.setData({

        page: 1,

        ani1: ''

      });

    }, 500)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})